package com.atlige.gzmall.order.controller;

import com.atlige.gzmall.order.entity.OrderEntity;
import com.atlige.gzmall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Jeremy Li
 * @data 2021/4/10 - 17:29
 */
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num", defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if (i % 2 == 0){
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setReceiverName("hello" + i);
                orderEntity.setCreateTime(new Date());
                // 如果发送的消息是一个对象，我们会使用序列化机制，将对象写出去，对象必须实现Serializable
                // 如果让发送出去的对象,可以是一个json
                //rabbitTemplate.convertAndSend("hello.java.exchange", "hello.java", "hello world");
                rabbitTemplate.convertAndSend("hello.java.exchange", "hello.java", orderEntity);
            }else {
                OrderReturnReasonEntity returnReasonEntity = new OrderReturnReasonEntity();
                returnReasonEntity.setId(1L);
                returnReasonEntity.setName("你好 -->" + i);
                returnReasonEntity.setCreateTime(new Date());
                rabbitTemplate.convertAndSend("hello.java.exchange", "hello22.java", returnReasonEntity);
            }
        }
        return "Ok";
    }

}
